from turtle import pd
import re


# 读取Excel文件
def read_excel():
    return pd.read_excel('F:/py/UserInfo.xlsx')


# 检查用户名是否存在
def check_username_exists(df, username):
    return df['用户名'].str.contains(username).any()


# 检查用户名长度
def check_username_length(username):
    return 6 <= len(username) <= 18


# 检查密码是否包含字母和数字
def check_password_complexity(password):
    return re.match(r'^(?=.*[a-zA-Z])(?=.*\d).+$', password) is not None


# 检查密码长度
def check_password_length(password):
    return 6 <= len(password) <= 20


# 检查两次密码是否一致
def check_passwords_match(password1, password2):
    return password1 == password2


# 注册用户
def register(df, username, password1, password2):
    if check_username_exists(df, username):
        return "用户名已存在"
    if not check_username_length(username):
        return "用户名应该6-18位"
    if not check_password_complexity(password1):
        return "密码必须包含字母和数字"
    if not check_password_length(password1):
        return "密码长度为6-20位"
    if not check_passwords_match(password1, password2):
        return "两次密码不一致"
    # 这里可以添加代码将新用户添加到df和Excel文件中
    return "注册成功"


# 登录用户
def login(df, username, password):
    if (df['用户名'] == username) & (df['密码'] == password).any():
        return "用户名和密码正确"
    else:
        return "用户名和密码错误"


# 主函数
def main():
    read_excel()

    # 模拟用户注册
    print(register(df, 'newuser', 'Password123', 'Password123'))

    # 模拟用户登录
    print(login(df, 'existinguser', 'existingpassword'))


if __name__ == "__main__":
    main()